<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" v-if="hasPermission('base:material:add')"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" v-if="hasPermission('base:material:exportXls')">
          导出</a-button
        >
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls" v-if="hasPermission('base:material:importExcel')"
          >导入</j-upload-button
        >
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BasMaterialModal @register="registerModal" @success="handleSuccess"></BasMaterialModal>
  </div>
</template>

<script lang="ts" name="basMaterial-basMaterial" setup>
import { ref, computed, unref, reactive, toRaw } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage';
import BasMaterialModal from './components/BasMaterialModal.vue';
import { columns, searchFormSchema } from './BasMaterial.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl,getStreamDownload } from './BasMaterial.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { usePermission } from '/@/hooks/web/usePermission';
const { hasPermission } = usePermission();
const checkedKeys = ref<Array<string | number>>([]);
//注册model
const [registerModal, { openModal }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '料品管理',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 160,
      fixed: 'right',
    },
  },
  exportConfig: {
    name: '料品管理',
    url: getExportUrl,//getStreamDownload
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});

const [registerTable, { reload, setProps }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isDetail:true,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'base:material:edit', //通过权限指令控制显示（有权限显示/无权限不显示）
    },
  ];
}
/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        placement: 'left',
        confirm: handleDelete.bind(null, record),
      },
      auth: 'base:material:delete', //通过权限指令控制显示（有权限显示/无权限不显示）
    },
  ];
}
// 高级查询配置
const superQueryConfig = reactive({
  code: { title: '料品编号', view: 'text', type: 'string', order: 1 },
  name: { title: '料品名称', view: 'text', type: 'string', order: 1 },
  spec: { title: '规格', view: 'text', type: 'string', order: 1 },
  marbasclassName: { title: '料品分类名称', view: 'text', type: 'string', order: 1 }, 
  materialTypeAttribute:{ title: "料品形态属性", view: "list_multi", type: "number",  dictCode: "marbas_material_type_attribute", order: 1 },
  procurementControlMethod:{ title: "采购控制方法", view: "list_multi", type: "number",  dictCode: "marbas_procurement_control_method", order: 1 },
  planMethod:{ title: "计划方法", view: "list_multi", type: "number",  dictCode: "marbas_plan_method", order: 1 },
  weightedConversionOpportunity: { title: '加权转换率取值时机', view: 'list_multi', type: 'number', dictCode:"marbas_weighted_conversion_opportunity",order: 1 },
  predictiveControl: { title: '预测控制', view: 'list_multi', type: 'number', dictCode:"marbas_predictive_control", order: 1 },
  approvedTolerancePrinciple: { title: '核准允差原则', view: 'list_multi', type: 'number', dictCode:"marbas_approved_tolerance_principle",order: 1 },
  planBoundPrinciple: { title: '计划时界原则', view: 'list_multi', type: 'number', dictCode:"marbas_plan_bound_principle", order: 1 },
  ceilingPriceSource: { title: '最高限价来源', view: 'list_multi', type: 'number', dictCode:"marbas_ceiling_price_source", order: 1 },
  planBoundType: { title: '计划时界类型', view: 'list_multi', type: 'number', dictCode:"marbas_plan_bound_type", order: 1 },
  returnPricingSource: { title: '退货取价来源', view: 'list_multi', type: 'number', dictCode:"marbas_return_pricing_source", order: 1 },
  provideBoundType: { title: '发放时界类型', view: 'list_multi', type: 'number',  dictCode:"marbas_provide_bound_type", order: 1 },
  quotaMethod: { title: '配额方式', view: 'list_multi', type: 'number',  dictCode:"marbas_quota_method", order: 1 },
  bomControlMethod: { title: 'BOM控制方式', view: 'list_multi', type: 'number',  dictCode:"marbas_bom_control_method", order: 1 },
  quotaSupplier: { title: '配额供应商', view: 'list_multi', type: 'number',  dictCode:"marbas_quota_supplier", order: 1 },
  receivingProcedure: { title: '收货程序', view: 'list_multi', type: 'number',  dictCode:"marbas_receiving_procedure", order: 1 },
  projectMethod: { title: '规划方法', view: 'list_multi', type: 'number',  dictCode:"marbas_project_method", order: 1 },
  receivingMethod: { title: '收货方式', view: 'list_multi', type: 'number',  dictCode:"marbas_receiving_method", order: 1 },
  safetyStockMethod: { title: '安全存量法', view: 'list_multi', type: 'number',  dictCode:"marbas_safety_stock_method", order: 1 },
  supplySource: { title: '供应来源', view: 'list_multi', type: 'number',  dictCode:"marbas_supply_source", order: 1 },
  type: { title: '供应类型', view: 'list_multi', type: 'number', dictCode:"marbas_type", order: 1 },
  productionQuantitySelection: { title: '生产数量选择', view: 'list_multi', type: 'number', dictCode:"marbas_production_quantity_selection", order: 1 },
  purchaseEstimateAdvance: { title: '采购预提前期（天）', view: 'text', type: 'string',  order: 1 },
  purchaseHandleAdvance: { title: '采购处理提前期（天）', view: 'text', type: 'string',  order: 1 },
  purchaseAfterAdvance: { title: '采购后提前期（天）', view: 'text', type: 'string', order: 1 },
  regularAdvance: { title: '固定提前期（天）', view: 'text', type: 'string', order: 1 },
  changeAdvance: { title: '变动提前期（天）', view: 'text', type: 'string',  order: 1 },
  advanceBatch: { title: '提前期批量', view: 'text', type: 'string',  order: 1 },
  makeAdvance: { title: '制造提前期（天）', view: 'text', type: 'string', order: 1 },
  gatherAdvance: { title: '汇总提前期（天）', view: 'text', type: 'string', order: 1 },
  remark: { title: '备注', view: 'text', type: 'string', order: 1 },
  thirdPartyCode: { title: '第三方编码', view: 'text', type: 'string', order: 1 },
});
//执行查询
function handleSuperQuery(params) {
  setProps({ searchInfo: toRaw(params) });
  reload();
}
</script>

<style scoped>
</style>